package com.coordinate.servlets;

import com.coordinate.core.message.impl.CTextMessage;
import com.coordinate.core.type.impl.CLangText;
import com.devicemantra.coordinate.ds.tests.PMF;
import com.coordinate.services.CoordMailService;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.Iterator;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.http.*;

@SuppressWarnings("serial")
public class TwilioXmlGeneratorServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		
		System.out.println("Echo'ing Request from twilio"+req.toString());
		
	}
	
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
	throws IOException {
		PrintWriter out = resp.getWriter();
		resp.setContentType("text/xml");
		String lVmKey = req.getParameter("vm_key");
		String lText = "You have no new messages";
		PersistenceManager lPm = PMF.get().getPersistenceManager();
		try{
			CTextMessage lMsg = lPm.getObjectById(CTextMessage.class,lVmKey);
			System.out.println("Got Message :" + lMsg);
			lText = lMsg.getLangText("en").getText().getValue();
			System.out.println("The text retrieved is " + lText);
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return;
		}
		finally{
			lPm.close();
		}
		
		
		out.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
		out.write("<Response> ");
		out.write("<Say>A Message from the Ministry of Sociall Affairs. United Arab Emirates.  "+lText+"</Say>");
		//out.write("<Play>http://egov-innovations.appspot.com/GetVm.wav?vm_key="+lVmKey+"</Play>");
		out.write("</Response>");
		out.close();
		
	}
}

